qui(
    function (require, exports, module) {
        VOD.Advert = (function () {
            var st = 1;
            var sLayer = '<div class="transparent_class"></div>';
            var FFSq = [];
            var add = [];
            var del = [];
            function DealFFSq(id) {
                for (var i in FFSq) {
                    if (FFSq[i] == id) {
                        FFSq.splice(i, 1);
                        return;
                    }
                }
                FFSq.push(id);
            }

            function popFFSq(id) {
                for (var i in FFSq) {
                    var temp = FFSq[i].split('|')
                    if (temp[0] == id) {
                        FFSq.splice(i, 1);
                        return;
                    }
                }
            }

            var tc = '';

            function add_firt_frame(obj, layer) {
                tc = $(obj).parents(".text-center");
                if ($(tc).find(".transparent_class").attr("class") == 'transparent_class') {
                    $(tc).find(".transparent_class").remove();
                } else {
                    $(tc).find('.btn-group').before(layer);
                }

                var id = $(tc).find("span").text();

                DealFFSq(id);
            }

            function layer(type) {
                var sLayer = '';
                if (type == 'ad')
                    sLayer = '<div class="transparent_class background-blue"></div>';
                else if (type == 'perspectiveImg')
                    sLayer = '<div class="transparent_class background-red"></div>';
                return sLayer;
            }

            //去除广告数据
            function      removeAdData   (stime, etime, needRemoveDelData) {
                stime = parseInt(stime);
                etime = parseInt(etime);
                //subData = [];
                var settings = VOD.Dao.settings;
               var  needDatas = [];
                for (i = stime; i <= etime; i++) {
                    needDatas.push(i);
                }

                if (needRemoveDelData && settings.delList.length) {
                    $.each(settings.delList, function (k, v) {
                        v = parseInt(v);
                        if (stime <= v && v <= etime) {
                            for (var i = 0; i < needDatas.length; i++) { //删除v的元素
                                if (needDatas[i] == v) {
                                    needDatas.splice(i, 1);
                                    break;
                                }
                            }
                        }
                    })
                }
                return needDatas;
            }

            var identifyImgOptType = {
                1: {
                    "dropList": {
                        "ad": "标记为广告",
                        "perspectiveImg": "标记为片头"
                    },
                    "desc": ""
                }, // 手动标记列表 标记 1 -> 2
                2: {
                    "dropList": {
                        "cancel": "取消标记"
                    },
                    "desc": "手动标记,待提交"
                }, // 手动标记取消 取消 2 -> 1 提交已表示帧入库 2 -> 3
                3: {
                    "dropList": {
                        "del": "从库中删除标记"
                    },
                    "desc": "已提交"
                }, // 手动标记删除 从库中删除标记触发 3 -> 4
                4: {
                    "dropList": {
                        "cancel": "取消删除"
                    },
                    "desc": "删除标记,待提交"
                }, // 手动标记删除提交 取消 4->3 提交已表示帧入库->7
                5: {
                    "dropList": {
                        "del": "从库中删除标记"
                    },
                    "desc": "自动标记"
                }, // 删除自动标记  删除自动标记触发->6
                6: {
                    "dropList": {
                        "cancel": "取消删除"
                    },
                    "desc": "删除自动标记,待提交"
                }, // 删除自动标记提交 取消->5 提交已表示帧入库->7
                7: {
                    "dropList": {},
                    "desc": "删除标记,已提交"
                }
            }

// 获取下一个状态
            function getNextState(num, opt) {
                var rt = 0;
                if (num == 1)
                    rt = 2;
                else if (num == 2 && opt == 'cancel')
                    rt = 1;
                else if (num == 2 && opt == '')
                    rt = 3;
                else if (num == 3)
                    rt = 4;
                else if (num == 4 && opt == 'cancel')
                    rt = 3;
                else if (num == 4 && opt == '')
                    rt = 7;
                else if (num == 5)
                    rt = 6;
                else if (num == 6 && opt == 'cancel')
                    rt = 5;
                else if (num == 6 && opt == '')
                    rt = 7;
                return rt;
            }

// 识别操作类型产生
            function identifyImgOpt(num) {
                var li = desc = '';
                var desc = identifyImgOptType[num]['desc'];
                if (num == 7) {
                    num = 1;
                    desc = identifyImgOptType[7]['desc'];
                }

                if (identifyImgOptType[num]['dropList'] != '') {
                    for (var i in identifyImgOptType[num]['dropList']) {
                        li += '<li><a href="javascript:void(0);" onclick="VOD.Advert.perspectiveOpt(this)" mark-opt="' + i + '" data-state="' + num + '">' + identifyImgOptType[num]['dropList'][i] + '</a></li>'
                    }
                }

                return {
                    "li": li,
                    "desc": desc
                }
            }

            var chars = ['0', '1', '2'];

            function generateMixed(n) {
                var res = "";
                for (var i = 0; i < n; i++) {
                    var id = Math.ceil(Math.random() * 3);
                    res += chars[id];
                }
                return res;
            }

            function changState(obj) {
                var $this = $(obj + ">li:first>a:first");
                var num = $this.attr('data-state');
                //mark = $this.attr('mark-opt');

                var nextState = getNextState(num, '');
                var opt = identifyImgOpt(nextState);

                var dropList = $this.parents(".dropList");
                $(dropList).html(opt.li);

                $(dropList).parents(".text-center").find(".desc").text(opt.desc);

                if (nextState == 1 || nextState == 7) {
                    $(dropList).parents(".text-center").find(".transparent_class").remove();
                }

            }

            function perspectiveOpt(obj) {
                var $this = $(obj);
                var num = $this.attr('data-state');
                var mark = $this.attr('mark-opt');
                var nextState = getNextState(num, mark);
                var opt = identifyImgOpt(nextState);

                var dropList = $this.parents(".dropList");
                $(dropList).html(opt.li);

                $(dropList).parents(".text-center").find(".desc").text(opt.desc);

                var lye = layer(mark);
                if (lye != '') {
                    $(dropList).parents(".text-center").find(".transparent_class").remove();
                    $(dropList).parents(".text-center").find('.btn-group').before(lye);
                }

                if (nextState == 1 || nextState == 7) {
                    $(dropList).parents(".text-center").find(".transparent_class").remove();
                }

                var id = $(dropList).parents(".text-center").find("span").text();

                //  加入添加
                if (nextState == 2)
                    FFSq.push(id + '|add');
                else if (nextState == 4 || nextState == 6) //  加入删除
                    FFSq.push(id + '|del');

                //  加入取消操作的图下
                if (nextState == 1 || nextState == 3 || nextState == 5)
                    popFFSq(id);

                //console.log(FFSq);

                if (mark == 'ad') {
                }
                else if (mark == 'perspectiveImg') {
                }

            }

            function FFSumbit() {
               var taskid = VOD.Dao.settings.taskid;
               var d_type = $('input[name=d_imgType]').val();
                if (taskid < 1) {
                    //alert('任务ID为空,不能提交!');
                    VOD.Plugin.tips("error", "任务ID为空,不能提交!");
                    return false;
                }

                if (!FFSq.length) {
                    //alert('请先标记图片再提交!');
                    VOD.Plugin.tips("error", "请先标记图片再提交");
                    return false;
                }
                add = [];
                del = [];
                var sListAdd  = '[', sListDel = '[';

                for (var i = 0; i < FFSq.length; i++) {
                    var temp = FFSq[i].split('|')
                    if (temp[1] == 'add') {
                        add.push(temp[0]);
                        sListAdd += temp[0] + ',';
                    } else if (temp[1] == 'del') {
                        del.push(temp[0]);
                        sListDel += temp[0] + ',';
                    }
                }

                if ((sListAdd.length - 1) != 0)
                    sListAdd = sListAdd.substring(0, sListAdd.length - 1); //去除逗号
                sListAdd += ']';

                if ((sListDel.length - 1) != 0)
                    sListDel = sListDel.substring(0, sListDel.length - 1); //去除逗号
                //console.log(sListDel);
                sListDel += ']';
                /*
                 if(add.length!==0)
                 sListAdd='无添加';

                 if(del.length!==0)
                 sListDel='无删除';
                 */
                // sList = sList.substring(0,sList.length-1);//去除逗号
                // sList += ']';

                if (confirm('广告标示图片添加: ' + sListAdd + "\n" + '广告标示图片删除: ' + sListDel) == false){
                    return;
                }


                var actionType = ['add', 'del'];
                var sList = null;
                var paramsData = VOD.Dao.getParamsData();
                var dotData = VOD.Dao.getDotData();
                for (var j = 0; j < actionType.length; j++) {
                    if (actionType[j] == 'add') {
                        if (add.length == 0)
                            continue;
                        sList = sListAdd;
                    } else if (actionType[j] == 'del') {
                        if (del.length == 0)
                            continue;
                        sList = sListDel;
                    }

                    var pData = {
                        'code':dotData.channel_name,
                        'key': taskid,
                        'list': sList,
                        'imgtype': 1,
                        'from_client': true,
                        'action': actionType[j]
                    };

                    $.ajax({
                        type: "post",
                        url: dotData.adUrl,
                        data: pData,
                        success: function (data) {
                            if (data.error != 0) {
                                //alert(data.error);
                                VOD.Plugin.tips("error", data.error);
                                return false;
                            } else {
                            }
                        },
                        dataType: "json",
                        error: function () {
                            //alert('网络错误');
                            VOD.Plugin.tips("error", '网络错误');
                        }
                    });
                }

                for (var k in add) { //console.log(add[k]);
                    changState("#ul-position-" + add[k]);

                }
                add = [];

                for (var k in del) {
                    changState("#ul-position-" + del[k]);

                }

                del = [];

                //alert('提交成功');
                VOD.Plugin.tips("success", '提交成功');
                //add = del = 0;
                FFSq = [];
            }

            function clearCache(){
                FFSq = [];
                add = [];
                del = [];
            }

            function addAd2Queue  (dataSubmit) {
                for (var i = parseInt(dataSubmit.sTime); i <= dataSubmit.eTime; i++) {
                    VOD.Dao.settings.push(i);
                }
                var uniqueArr = [];
                $.each( VOD.Dao.settings, function (i, el) { //去重复
                    if ($.inArray(el, uniqueArr) === -1)
                        uniqueArr.push(el);
                });
                VOD.Dao.settings.delList = uniqueArr;
            }
            //去除广告数据
              function removeUnselectedData(options) {

              var  needDatas = [];

                if (!options)
                    return;

                $.each(options.split(','), function (i, v) {
                    var frameNumberArr = v.split('_');
                    for (i = parseInt(frameNumberArr[0]); i <= parseInt(frameNumberArr[1]); i++) {
                        needDatas.push(i);
                    }
                });

                return needDatas;
            }
            return {
                DealFFSq: DealFFSq,
                popFFSq: popFFSq,
                identifyImgOptType: identifyImgOptType,
                layer: layer,
                add_firt_frame: add_firt_frame,
                perspectiveOpt: perspectiveOpt,
                clearCache:clearCache,
                addAd2Queue:addAd2Queue,
                removeAdData:removeAdData,
                removeUnselectedData:removeUnselectedData
            }
        })()
    }
);